<?php
/**
 * @file
 * Deletes a deed from the database.
 *
 * The deed ID of the deed to delete is retreived from the POST request. The
 * response is returned in plain text:
 *  - Success : {DEED_ACTION_OK}{DELIMITER_DEED_ACTION_RESPONSE}<deed id>
 *  - Failure : {DEED_ACTION_KO}{DELIMITER_DEED_ACTION_RESPONSE}<error message>
 *
 * Usage:
 *  - Submit a POST request to this page.
 *
 * GET Parameter(s):
 *  - None.
 *
 * POST Parameter(s):
 *  - {ID_FIELD_ID_DEED} : Deed ID of the deed to delete.
 *
 * Global variable(s) set:
 *  - None.
 *
 * Global variable(s) unset:
 *  - None.
 *
 * Exception(s):
 *  - None.
 *
 * This file includes/requires:
 *  - h_greenindeed.php
 *  - h_deed.php
 *  - db_mysql_greenindeed.php
 *  - db_mysql_connect.php
 *  - db_mysql_disconnect.php
 */

// header file(s) ==============================================================
require_once "h_greenindeed.php";
require_once "h_deed.php";


// implementation ==============================================================
try {
  // get id of deed to delete
  $id_deed = $_POST[ID_FIELD_ID_DEED];

  // connect to database
  require_once "db_mysql_greenindeed.php";
  require_once $PATH_COMMON . "php/db_mysql_connect.php";

  // delete deed
  $result    = mysql_query("delete from DEEDS where DEED_ID = " . $id_deed);
  $sql_error = mysql_error();

  // disconnect from database
  require_once $PATH_COMMON . "php/db_mysql_disconnect.php";

  // test result
  if (true == $result) {
    // return ok flag and id of deleted deed
    echo DEED_ACTION_OK . DELIMITER_DEED_ACTION_RESPONSE . $id_deed;
  } else {
    // return ko flag and error message
    echo DEED_ACTION_KO . DELIMITER_DEED_ACTION_RESPONSE . $sql_error;
  }
} catch (Exception $ex) {
  // return ko flag and exception message
  echo DEED_ACTION_KO . DELIMITER_DEED_ACTION_RESPONSE . $ex->getMessage();
}
?>
